package com.aaa.product.service.impl;

import com.aaa.product.dao.TbActInfMapper;
import com.aaa.product.dao.TbPrdInfoMapper;
import com.aaa.product.entity.TbActInf;
import com.aaa.product.entity.TbPrdInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aaa.product.entity.TbActProduct;
import com.aaa.product.dao.TbActProductMapper;
import com.aaa.product.service.TbActProductService;
@Service
public class TbActProductServiceImpl extends ServiceImpl<TbActProductMapper, TbActProduct> implements TbActProductService{

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    TbPrdInfoMapper tbPrdInfoMapper;
    @Resource
    TbActInfServiceImpl tbActInfService;

    /**
     * 修改活动商品的价格
     *
     * @param tbActProduct
     */
    @Override
    public boolean updateTbActProductPrice(TbActProduct tbActProduct) {
        return updateById(tbActProduct);
    }

    /**
     * 添加活动商品
     *
     * @param arr
     */
    @Override
    public boolean saveActivityProduct(Object[] arr) {
        boolean flag = true;
        String tid = arr[0].toString();
        for (int i = 1 ; i < arr.length; i++){
            Map<String,Object> map = (Map<String, Object>) arr[i];
            TbActProduct tbActProduct = new TbActProduct();
            tbActProduct.setTid(tid);
            QueryWrapper<TbActInf> tbActInfQueryWrapper = new QueryWrapper<>();
            tbActInfQueryWrapper.eq("tid",tid);
            List<TbActInf> list = tbActInfService.list(tbActInfQueryWrapper);
            TbActInf tbActInf = new TbActInf();
            if(list.size()>0){
                tbActInf = list.get(0);
            }
            tbActProduct.setStartTime(tbActInf.getStartTime());
            tbActProduct.setFinishTime(tbActInf.getFinishTime());
            tbActProduct.setPid(map.get("tid").toString());
            tbActProduct.setStore(Integer.valueOf(map.get("store").toString()));
            tbActProduct.setStore(Integer.valueOf(map.get("newStore").toString()));
            tbActProduct.setOldPrice(Double.valueOf(map.get("price").toString()));
            tbActProduct.setNewPrice(Double.valueOf(map.get("price").toString()) * 0.9);
            flag = save(tbActProduct);
        }
        return flag;
    }

    /**
     * 减活动商品的库存
     * @return
     */
    @Override
    public boolean subtractStore(Object[] arr) {
        int a = 0 ;
        for (int i = 0 ; i < arr.length; i++){
            System.out.println(arr[i]);
            Boolean b  = tbPrdInfoMapper.subtractStore(arr[i]);
            if (b){
                a++;
            }
        }
        if (a == arr.length){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 查询活动商品的详细信息
     *
     * @param tid
     * @param pid
     * @return
     */
    @Override
    public TbActProduct selectTbActProductByTidAndPid(String tid, String pid) {
        QueryWrapper<TbActProduct> wrapper = new QueryWrapper<>();
        wrapper.eq("tid",tid);
        wrapper.eq("pid",pid);
        List<TbActProduct> list = list(wrapper);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 获取一个进行的活动详情中的某个商品的详情
     *
     * @param time
     * @param pid
     */
    @Override
    public TbActProduct selectTbActInfByNowTimeAndPid(String time, String pid) {
        return (TbActProduct) redisTemplate.boundHashOps(time).get(pid);
    }

}
